package com.party.core.service.member.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.core.dao.read.member.MemberGroupPermissionReadDao;
import com.party.core.dao.write.member.MemberGroupPermissionWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.member.MemberGroupPermission;
import com.party.core.service.member.IMemberGroupPermissionService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName MemberGroupPermissionService
 * @Description 机构数据权限
 * @Author yifeng
 * @Date 2019/11/26 9:49
 **/
@Service
public class MemberGroupPermissionService implements IMemberGroupPermissionService {
    @Autowired
    private MemberGroupPermissionReadDao memberGroupPermissionReadDao;

    @Autowired
    private MemberGroupPermissionWriteDao memberGroupPermissionWriteDao;

    @Override
    public String insert(MemberGroupPermission memberGroupPermission) {
        BaseModel.preInsert(memberGroupPermission);
        boolean result = memberGroupPermissionWriteDao.insert(memberGroupPermission);
        if (result) {
            return memberGroupPermission.getId();
        }
        return null;
    }

    @Override
    public boolean update(MemberGroupPermission memberGroupPermission) {
        memberGroupPermission.setUpdateDate(new Date());
        return memberGroupPermissionWriteDao.update(memberGroupPermission);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberGroupPermissionWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberGroupPermissionWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberGroupPermissionWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberGroupPermissionWriteDao.batchDelete(ids);
    }

    @Override
    public MemberGroupPermission get(String id) {
        return memberGroupPermissionReadDao.get(id);
    }

    @Override
    public List<MemberGroupPermission> listPage(MemberGroupPermission memberGroupPermission, Page page) {
        return memberGroupPermissionReadDao.listPage(memberGroupPermission, page);
    }

    @Override
    public List<MemberGroupPermission> list(MemberGroupPermission memberGroupPermission) {
        return memberGroupPermissionReadDao.listPage(memberGroupPermission, null);
    }

    @Override
    public List<MemberGroupPermission> batchList(Set<String> ids, MemberGroupPermission memberGroupPermission, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return memberGroupPermissionReadDao.batchList(ids, new HashMap<>(), page);
    }

    @Override
    public Integer getIsShowAll(String type, String memberId, String partnerId, List<String> targetIds) {
        Integer isShowAll = null;

        MemberGroupPermission p = new MemberGroupPermission();
        p.setMemberId(memberId);
        p.setPartnerId(partnerId);
        p.setType(type);
        List<MemberGroupPermission> permissions = list(p);
        for (MemberGroupPermission permission : permissions) {
            isShowAll = permission.getIsShowAll();
            break;
        }
        if (null != isShowAll && YesNoStatus.NO.getCode().equals(isShowAll)) {
            List<String> transform = LangUtils.transform(permissions, input -> input.getTargetId());
            targetIds.addAll(transform);
        }
        return isShowAll;
    }

    @Override
    public boolean deleteBySearch(String type, String memberId, String partnerId) {
        return memberGroupPermissionWriteDao.deleteBySearch(type, memberId, partnerId);
    }
}
